// password.js
const app = getApp()
const mobileReg = /^(13|15|17|18|14|19|16)[0-9]{9}$/
const passwordReg = /^[A-Za-z0-9\.@_]{6,24}$/
import { requestApi } from '../../../api'
import md5 from '../../../utils/md5.min'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countryArray: [],
    countryIndex: 0,
    mobilePhone: '',
    codeText: '获取验证码',
    codeRequestFlag: false,
    requestFlag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCountryCode()
  },

  getMobile: function (e) {
    this.setData({
      mobilePhone: e.detail.value
    })
  },

  getVcode: function (e) {
    let mobilePhone = this.data.mobilePhone || ''
    let countryArray = this.data.countryArray
    let countryIndex = this.data.countryIndex
    let countryCode = countryArray[countryIndex].countrycode
    let send_timestamp = new Date().getTime();
    let send_sign = md5('4a68cd1c04adcbd49e599a38f6f82b5a' + send_timestamp + mobilePhone);
    let options = {
      url: requestApi.vCodeUrl,
      method: 'POST',
      data: {
        'send_sign': send_sign,
        'send_timestamp': send_timestamp,
        'receiver': mobilePhone,
        'countrycode': countryCode,
        'service_type': '4',
        'receiver_type': '1'
      },
    }

    if (mobilePhone == '') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '手机号填写有误',
      })
      return false
    }

    if (this.data.codeRequestFlag) return false
    app.request(
      options,
      (data) => {
      },
      (error) => {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: error,
        })
      })

    this.countDown()
  },

  countDown: function () {
    let that = this
    let codeText = ''
    let time = 60

    that.setData({
      codeRequestFlag: true,
    })

    let countDown = setInterval(function () {
      time = time - 1
      codeText = time + '秒后重新获取'
      that.setData({
        codeText: codeText,
      })
    }, 1000)

    setTimeout(function () {
      clearInterval(countDown)

      that.setData({
        codeRequestFlag: false,
        codeText: '点击获取验证码',
      })
    }, 60000)
  },

  formSubmit: function (e) {
    let submitData = e.detail.value
    let countryArray = this.data.countryArray
    let countryIndex = this.data.countryIndex
    let countryCode = countryArray[countryIndex].countrycode || ''
    let mobilePhone = submitData.mobilephone || ''
    let vcode = submitData.vcode || ''
    let password = submitData.password || ''
    let options = {
      url: requestApi.forgetPasswordUrl,
      method: 'POST',
      data: {
        'mobilephone': mobilePhone,
        'vcode': vcode,
        'countrycode': countryCode,
        'password': password,
      },
    }

    if (!mobileReg.test(mobilePhone)) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '手机号格式有误，请重试',
      })
      return false
    }

    if (this.data.requestFlag) return false
    this.setData({
      requestFlag: true,
    })

    app.request(
      options,
      (data) => {
        wx.showToast({
          title: '重置成功！',
          duration: 1000,
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/personalList/personalList',
          })
        }, 500)
      },
      (error) => {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: error,
        })
      }, () => {
        this.setData({
          requestFlag: false,
        })
      })
  },

  pickerChange: function (e) {
    this.setData({
      countryIndex: e.detail.value
    })
  },

  getCountryCode: function () {
    // let url = API_URL
    let url = requestApi.countryCodeUrl
    let countryArray

    app.request({
      url: url,
      login: false,
      data: {},
    }, (data) => {
      countryArray = data.original.countrycodes

      this.setData({
        countryArray: countryArray
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    })
  }
})